//
//  GFPandectTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFPandectTableViewCell.h"

@interface GFPandectTableViewCell ()
@property (nonatomic, strong) UIImageView *titleImage;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UILabel *subLabel;
@property (nonatomic, strong) UILabel *levelLabel;
@end

@implementation GFPandectTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.titleImage = [[UIImageView alloc] init];
        self.titleImage.layer.masksToBounds = YES;
        self.titleImage.layer.cornerRadius = ScreenWidth / 375 * 30;
        [self.contentView addSubview:self.titleImage];
        
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        
        
        self.countLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.countLabel];
        
        
        self.subLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.subLabel];
        
        
        self.levelLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.levelLabel];
      
    }
    
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleImage.frame = CGRectMake(ScreenWidth / 375 * 20, ScreenWidth / 375 * 20, ScreenWidth / 375 * 60, ScreenWidth / 375 * 60);
    
    //    CGFloat width1 = [CalculateWidthOrHeigth stringWidth:self.panModel.name withHeigth:30 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    self.titleLabel.frame = CGRectMake(ScreenWidth / 375 * 90, ScreenWidth / 375 * 20, ScreenWidth / 375 * 230, ScreenWidth / 375 * 30);
    self.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    self.titleLabel.font = [UIFont systemFontOfSize:17];
    
    CGFloat width2 = [GFCalculateWidthOrHeigth stringWidth:self.countLabel.text withHeigth:ScreenWidth / 375 * 30 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    
    self.countLabel.frame = CGRectMake(ScreenWidth / 375 * 90, ScreenWidth / 375 * 60, width2, ScreenWidth / 375 * 20);
    self.countLabel.textColor = [UIColor redColor];
    
    CGFloat width3 = [GFCalculateWidthOrHeigth stringWidth:self.subLabel.text withHeigth:ScreenWidth / 375 * 20 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    self.subLabel.frame = CGRectMake(ScreenWidth / 375 * 90 + width2, ScreenWidth / 375 * 60, width3, ScreenWidth / 375 * 20);
    self.subLabel.font = [UIFont systemFontOfSize:15];
    
    self.levelLabel.frame = CGRectMake(ScreenWidth / 375 * 330, ScreenWidth / 375 * 40, ScreenWidth / 375 * 20, ScreenWidth / 375 * 20);
    self.levelLabel.layer.masksToBounds = YES;
    self.levelLabel.layer.cornerRadius = ScreenWidth / 375.0 * 10;
}

- (void)setPanModel:(GFPandectModel *)panModel {
    if (_panModel != panModel) {

        _panModel = panModel;
    }
    if (panModel.thumb_image_url) {
        [self.titleImage setImageWithURL:[NSURL URLWithString:panModel.thumb_image_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
    }

    
    self.titleLabel.text = panModel.name;
    
    if (panModel.health_light == 1) {
        self.levelLabel.backgroundColor = [UIColor colorWithRed:77 / 255.0 green:152 / 255.0 blue:17 / 255.0 alpha:1];
    } else if (panModel.health_light == 2) {
        self.levelLabel.backgroundColor = [UIColor colorWithRed:223 / 255.0 green:166 / 255.0 blue:14 / 255.0 alpha:1];
    } else {
        self.levelLabel.backgroundColor = [UIColor colorWithRed:216 / 255.0 green:13 / 255.0 blue:27 / 255.0 alpha:1];
    }
    
}

- (void)setItemNumber:(NSString *)itemNumber {
    
    if (_itemNumber != itemNumber) {

        _itemNumber = itemNumber;
    }
    
    if (self.itemNumber.intValue < 2) {
        
        self.countLabel.text = self.panModel.calory;
        
        NSString *str = [NSString stringWithFormat:@"大卡/%@克", self.panModel.weight];
        
        self.subLabel.text = str;
        
    } else if (self.itemNumber.intValue == 2) {
        
        self.countLabel.text = self.panModel.protein;
        
        NSString *str = [NSString stringWithFormat:@"克/%@克", self.panModel.weight];
        
        self.subLabel.text = str;
    } else {
        
        if ([self.panModel.fat isEqualToString:@""]) {
            
            self.countLabel.text = @"0.0";
        } else {
            
            self.countLabel.text = self.panModel.fat;
        }
        
        NSString *str = [NSString stringWithFormat:@"克/%@克", self.panModel.weight];
        
        self.subLabel.text = str;
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
